package com.grandtech.insurance.common.utils.collect;

import com.grandtech.insurance.common.db.jdbc.sql.ReflectUtil;
import com.grandtech.insurance.common.utils.ObjectUtil;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

/**
 * 集合查询
 *
 * @param <T>
 */
public class CollectQuery<T>  {

    private List<T> list;

    private T t;

    private MultiCriteria multiCriteria;

    public CollectQuery(List<T> list, MultiCriteria multiCriteria) {
        this.list = list;
        this.multiCriteria = multiCriteria;
    }

    public List<T> query() {
        if (multiCriteria == null) return list;
        List<T> res = new ArrayList<>();
        List<Criteria> criteriaList = multiCriteria.getListCriteria();
        Criteria criteria;
        String fieldName;
        String linkName;
        Object fieldValue;
        Object varValue;
        boolean flag = false;
        if (multiCriteria.hasQuery()) {
            for (int i = 0; i < list.size(); i++) {
                t = list.get(i);
                for (int j = 0; j < criteriaList.size(); j++) {
                    criteria = criteriaList.get(j);
                    fieldName = criteria.getFieldName();
                    linkName = criteria.getLinkName();
                    fieldValue = criteria.getFieldValue();
                    varValue = ReflectUtil.getValByNumberVar(t, fieldName);
                    if (linkName.trim().contains("=")) {
                        flag = ObjectUtil.baseTypeIsEqual(varValue, fieldValue, false);
                    }
                    if (linkName.trim().contains("like")) {
                        flag = ObjectUtil.baseTypeLike(varValue, fieldValue);
                    }
                    if (linkName.trim().contains("<>")) {
                        flag = !ObjectUtil.baseTypeIsEqual(varValue, fieldValue, false);
                    }
                }
                if (flag) {
                    res.add(t);
                }
            }
        } else {
            res.addAll(list);
        }
        if (multiCriteria.hasOrder()) {
            for (int j = 0; j < criteriaList.size(); j++) {
                criteria = criteriaList.get(j);
                fieldName = criteria.getFieldName();
                linkName = criteria.getLinkName();
                fieldValue = criteria.getFieldValue();
                if (linkName.contains("order by")) {
                    Collections.sort(res, new Comparator1(fieldName, fieldValue.toString()));
                    break;
                }
            }
        }
        return res;
    }


    public class Comparator1 implements Comparator<T> {

        String fieldName;

        String fieldValue;

        public Comparator1(String fieldName, String fieldValue) {
            this.fieldName = fieldName;
            this.fieldValue = fieldValue;
        }

        @Override
        public int compare(T t1, T t2) {
            Object var1 = ReflectUtil.getValByNumberVar(t1, fieldName);
            Object var2 = ReflectUtil.getValByNumberVar(t2, fieldName);
            if (fieldValue.equals("desc")) {
                return ObjectUtil.compareTo(var1, var2);
            } else {
                return ObjectUtil.compareTo(var2, var1);
            }
        }
    }
}
